﻿using System;
using System.Collections.Generic;

using DigitalQuran.Data.Metadata;

namespace DigitalQuran.Collection
{
    /// <summary>
    /// Collection of <see cref="DigitalQuran.Group"/>
    /// </summary>
    public class GroupCollection : ReadOnlyCollection<Group>    
    {
        /// <summary>
        /// Total Groups in Qur'an
        /// </summary>
        public const int TotalGroups = 60;

        internal GroupCollection() : this(0, TotalGroups) { }

        int startIndex = 0;
        /// <summary>
        /// Creates a subset of <see cref="DigitalQuran.Collection.GroupCollection"/>
        /// </summary>
        /// <param name="startIndex">First Group Index</param>
        /// <param name="count">Group count to include</param>
        public GroupCollection(int startIndex, int count)
            : base(count)
        {
            this.startIndex = startIndex;
        }

        public override Group this[int index]
        {
            get
            {
                if (index < Count)
                    return _Groups[index + startIndex];
                else
                    throw new IndexOutOfRangeException();
            }
        }

        public override IEnumerator<Group> GetEnumerator()
        {
            for (int i = 0; i < Count; i++)
            {
                yield return _Groups[i + startIndex];
            }
        }

        static Group[] _Groups = new Group[TotalGroups];
        internal static void LoadData(IEnumerator<ValueSpan> e)
        {
            for (int i = 0; i < TotalGroups; i++)
            {
                e.MoveNext();
                _Groups[i] = new Group(i, e.Current.LowerBound, e.Current.Count);
            }
        }
    }
}